<?php

namespace app\api\model;

class LiveRankingHour extends Common
{
    static public function index($user_id, $money)
    {
        $current_time = time();

        $data = self::get(['user_id' => $user_id]);

        if ($data) {
            self::update(['id' => $data->id, 'money' => $data->money + $money, 'update_time' => $current_time]);
        } else {
            $count = self::where(['id' => ['gt', 0]])->count();

            if ($count >= 50) {
                $end = self::where(['id' => ['gt', 0]])->order('money asc')->find();

                if ($money > $end['money']) {
                    self::update(['id' => $end->id, 'user_id' => $user_id, 'money' => $money, 'create_time' => $current_time, 'update_time' => $current_time]);
                }
            } else {
                self::create(['user_id' => $user_id, 'money' => $money, 'create_time' => $current_time, 'update_time' => $current_time]);
            }
        }
    }
}
